package dshop.action.admin;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import appbase.jfinal.ext.ApiBaseController;
import appbase.jfinal.ext.ModelKit;
import appbase.jfinal.ext.RecordKit;
import dshop.AppConst;
import dshop.interceptor.ApiAuthInterceptor;
import dshop.interceptor.ApiReadBodyParamInterceptor;
import dshop.model.Area;
import dshop.model.Expert;
import dshop.model.company.Company;
import dshop.model.member.MemberCompany;
import dshop.model.member.MemberGeneralInfo;
import dshop.model.member.MemberVip;

/**
 * 智囊团/专家团管理
 */
@Before({ApiReadBodyParamInterceptor.class, ApiAuthInterceptor.class})
public class ExpertAction extends ApiBaseController{
	

	public void list()
	{
		String keyWord = getPara("keyWord", "").trim();
		String ckeckStatus = getPara("checkStatus", "").trim();
		
		//排序
		String columnProp = getPara("columnProp", "").trim();
		String columnOrder = getPara("columnOrder", "").trim().split("ending")[0];
		
		String select = " select e.id, e.account_id, e.tag, e.apply_date_time, e.check_status, e.check_status_explain, e.check_date_time, " 
					  + " m.gender, concat(m.last_name, m.first_name) as real_name, m.avatar, m.phone ";
		String where = " from bbs_expert_panel e "
					 + " inner join member_general_info m on m.account_id = e.account_id "
					 + " where 1 = 1 ";
		String orderBy = " ";
		
		List<Object> params = new ArrayList<Object>();
		if (!keyWord.isEmpty())
		{
			where += " and ( m.phone like ? or m.first_name like ?)";
			String keyWordParam = "%" + keyWord + "%";
			params.add(keyWordParam);
			params.add(keyWordParam);
		}
		if (!ckeckStatus.isEmpty())
		{
			where += " and check_status = ? ";
			params.add(ckeckStatus);
		}
		if(!columnOrder.isEmpty()) {
			orderBy += "order by "+columnProp+" "+columnOrder;
		}else {
			orderBy = "";
		}
		
		Page<Record> page = page(select, where + orderBy, params.toArray());
		List<Record> list = page.getList();
		
		MemberCompany.setCompanyIdToRs(list, "account_id");
		Company.setInfoToRs(list, "company_id");
		
		RecordKit.putDateStr(list, "apply_date_time", "apply_date_time_str", "yyyy-MM-dd HH:mm");
		RecordKit.putDateStr(list, "check_date_time", "check_date_time_str", "yyyy-MM-dd HH:mm");
		
		renderSuccess();
	}

	
	public void detail()
	{
		int expertId = getParaToInt("id");
		
		Expert expert = Expert.dao.findById(expertId);
		setAttr("expert", expert);
		
		int accountId = expert.getInt("account_id");
		
		MemberGeneralInfo member = MemberGeneralInfo.dao.findById(accountId);
		setAttr("member", member);
		
		MemberVip memberVip = MemberVip.dao.findById(accountId);
		if (memberVip == null)
		{
			memberVip = new MemberVip();
		}
		setAttr("memberVip", memberVip);
		
		Company company = null;
		Integer companyId = MemberCompany.getCompanyId(accountId);
		if (companyId != null)
		{
			company = Company.dao.findById(companyId);
			Area.setNameToM(company, "province_code, city_code, area_code", "province, city, area");
		}else
		{
			company = new Company();
		}
		setAttr("company", company);
		
		ModelKit.putDateFormat(expert, "apply_date_time", "apply_date_time_str", "yyyy-MM-dd HH:mm");
		ModelKit.putDateFormat(expert, "check_date_time", "check_date_time_str", "yyyy-MM-dd HH:mm");
		ModelKit.putDateFormat(memberVip, "start_date_time", "start_date_time_str", "yyyy-MM-dd HH:mm");
		ModelKit.putDateFormat(memberVip, "end_date_time", "end_date_time_str", "yyyy-MM-dd HH:mm");
		
		renderSuccess();
	}
	
	
	public void checkStatusPass()
	{
		int expertId = getParaToInt("id");
		String remark = getPara("remark", "").trim();
		String checkStatus = AppConst.CHECK_STATUS_PASS;
		
		Expert.setCheckStatus(expertId, checkStatus, remark);
		
		renderSuccess("成功设置为已通过");
	}
	
	
	public void checkStatusFail()
	{
		int expertId = getParaToInt("id");
		String remark = getPara("remark", "").trim();
		String checkStatus = AppConst.CHECK_STATUS_FAIL;
		
		Expert.setCheckStatus(expertId, checkStatus, remark);
		
		renderSuccess("已设置为不通过");
	}
	
}
